package com.zmn.oms.third.suning;


import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.third.suning.api.SuningOrderProduct;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 苏宁保外渠道常量定义类
 *
 * @author xiewenbing
 * @date 2021/01/05 17:00
 */
@Component
public class SuningConsts {

    /**
     * 渠道名称
     */
    public final static String CHANNEL_NAME = "苏宁保外";

    /**
     * 渠道对接开关
     */
    public static boolean POWER_ON = true;

    /**
     * APP KEY
     */
    public static String APP_KEY = "00800410";

    /**
     * APP SECRET
     */
    public static String APP_SECRET = "J6bBGUj6dicZ8RuLXI8cwA==";

    /**
     * API URL
     */
    public static String API_URL = "http://101.125.0.54:10008/asapi-web/api/v1?method=%s&app_key=%s&sign=%s";

    /**
     * 服务接口名称 - ASES订单信息传输接口
     */
    public final static String METHOD_CREATE_ORDER = "suning.fw.ffszlcreateorder.send";

    /**
     * 服务接口名称 - ASES订单信息改单接口
     */
    public final static String METHOD_MODIFY_ORDER = "suning.fw.ffszlmodifyorder.send";

    /**
     * 服务接口名称 - 服务商反馈派工信息接口
     */
    public final static String METHOD_ORDER_DISTRIBUTE = "suning.fw.ffszlPgr.create";

    /**
     * 服务接口名称 - 服务商反馈现场签到信息接口
     */
    public final static String METHOD_ORDER_VIST_SIGN = "suning.fw.ffszlSignIn.create";

    /**
     * 服务接口名称 - 服务商反馈销单信息接口
     */
    public final static String METHOD_ORDER_DESTORY = "suning.fw.ffszlDestory.create";

    /**
     * 请求应答结果码 - 成功
     */
    public final static String RESPONSE_CODE_SUCCESS = "S";

    /**
     * 请求应答结果码 - 失败
     */
    public final static String RESPONSE_CODE_FAILED = "F";

    /**
     * 同步请求应答结果码 - 成功
     */
    public final static String SYNC_RESPONSE_CODE_SUCCESS = "Y";

    /**
     * 同步请求应答结果码 - 失败
     */
    public final static String SYNC_RESPONSE_CODE_FAILED = "N";

    /**
     * 订单状态码 - 派工
     */
    public final static String STATUS_CODE_DISTRIBUTE = "E0003";

    /**
     * 订单状态码 - 改约
     */
    public final static String STATUS_CODE_CHANGE_DUTYTIME = "E0004";

    /**
     * 订单状态码 - 完成
     */
    public final static String STATUS_CODE_COMPLETE = "E0006";

    /**
     * 订单状态码 - 取消
     */
    public final static String STATUS_CODE_CANCEL = "E0008";

    /**
     * 更新操作类型 - 修改
     */
    public final static String UPDATE_OPERATE_TYPE_CHANGE = "01";

    /**
     * 更新操作类型 - 取消
     */
    public final static String UPDATE_OPERATE_TYPE_CANCEL = "02";

    /**
     * 处理的订单服务类型
     */
    public final static String ACCEPT_ORDER_TYPE = "ZS12-生活服务订单";

    /**
     * 同步销单的fromSys - 字段
     */
    public final static String FIELD_FROM_SYS = "啄木鸟";

    /**
     * 维修措施代码 - 字段
     */
    public static String FIELD_REPAIR_CODE = "A02040009";

    /**
     * 维修措施描述 - 字段
     */
    public static String FIELD_REPAIR_DESC = "疏通管道";

    /**
     * 作业人员工号
     */
    public static String FIELD_MASTER_CODE = "RY00138521";

    /**
     * 作业人员名称
     */
    public static String FIELD_MASTER_NAME = "一龙";

    /**
     * 作业人员电话
     */
    public static String FIELD_MASTER_PHONE = "12112112100";

    /**
     * 供应商编码
     */
    public static String FIELD_SUPPLIER_CODE = "60017383";

    /**
     * 服务商品编码 1
     */
    public static String SERVICE_PRODUCT_CODE_1 = "520500590";

    /**
     * 服务商品编码 2
     */
    public static String SERVICE_PRODUCT_CODE_2 = "520500589";

    /**
     * 服务商品编码 3
     */
    public static String SERVICE_PRODUCT_CODE_3 = "520500593";

    /**
     * 服务商品编码 3
     */
    public static String SERVICE_PRODUCT_CODE_4 = "520500592";


    /**
     * 服务商品对应的产品列表
     */
    public static List<SuningOrderProduct> PRODUCT_LIST = new ArrayList<>(3);

    static {
        PRODUCT_LIST.add(SuningOrderProduct.builder().productId(12944).productName("洗菜池疏通上门服务（不拆）").prepayAmount(15000).build());
        PRODUCT_LIST.add(SuningOrderProduct.builder().productId(12945).productName("洗脸池疏通上门服务（不拆）").prepayAmount(15000).build());
        PRODUCT_LIST.add(SuningOrderProduct.builder().productId(12946).productName("马桶疏通上门服务（不拆）").prepayAmount(15000).build());
        PRODUCT_LIST.add(SuningOrderProduct.builder().productId(12954).productName("蹲坑疏通上门服务（不拆）").prepayAmount(15000).build());
    }

    /**
     * 通过服务商品编码获取产品信息
     * @param serviceProductCode
     * @return
     */
    protected static SuningOrderProduct getOrderProduct(String serviceProductCode) {

        if (Objects.equals(serviceProductCode,SERVICE_PRODUCT_CODE_1)) {
            return PRODUCT_LIST.get(0);
        } else if (Objects.equals(serviceProductCode,SERVICE_PRODUCT_CODE_2)) {
            return PRODUCT_LIST.get(1);
        } else if (Objects.equals(serviceProductCode,SERVICE_PRODUCT_CODE_3)) {
            return PRODUCT_LIST.get(2);
        } else if (Objects.equals(serviceProductCode,SERVICE_PRODUCT_CODE_4)) {
            return PRODUCT_LIST.get(3);
        }

        return null;
    }


    /**
     * 通过服务商品编码获取产品id
     * @param serviceProductCode
     * @return
     */
    public static int getProductId(String serviceProductCode) {
/*

        SuningOrderProduct orderProduct = getOrderProduct(serviceProductCode);
        if (Objects.nonNull(orderProduct)) {
            return orderProduct.getProductId();
        }
*/

        return OrderConsts.CHANNEL_DISABLED_PRODUCT_ID;
    }

    /**
     * 通过服务商品编码获取服务商品名称
     * @param serviceProductCode
     * @return
     */
    public static String getServiceProductName(String serviceProductCode) {

        SuningOrderProduct orderProduct = getOrderProduct(serviceProductCode);
        if (Objects.nonNull(orderProduct)) {
            return orderProduct.getProductName();
        }

        return "";
    }

    /**
     * 通过服务商品编码获取渠道预付
     * @param serviceProductCode
     * @return
     */
    public static Integer getServiceProductPrepay(String serviceProductCode) {

        SuningOrderProduct orderProduct = getOrderProduct(serviceProductCode);
        if (Objects.nonNull(orderProduct)) {
            return orderProduct.getPrepayAmount();
        }

        return 0;
    }

    /************************************  下面是动态配置 *************************************/
    @NacosValue(value="${suning.api.powerOn:false}", autoRefreshed = true)
    public void setPowerOn(boolean powerOn) {
        POWER_ON = powerOn;
    }

    @NacosValue(value="${suning.api.url:http://101.125.0.54:10008/asapi-web/api/v1?method=%s&app_key=%s&sign=%s}", autoRefreshed = true)
    public void setApiUrl(String apiUrl) {
        API_URL = apiUrl;
    }

    @NacosValue(value="${suning.app.secret:J6bBGUj6dicZ8RuLXI8cwA==}", autoRefreshed = true)
    public void setAppSecret(String appSecret) {
        APP_SECRET = appSecret;
    }

    @NacosValue(value="${suning.app.key:00800410}", autoRefreshed = true)
    public void setAppKey(String appKey) {
        APP_KEY = appKey;
    }

    @NacosValue(value="${suning.field.repair.code:A02040009}", autoRefreshed = true)
    public void setFieldRepairCode(String repairCode) {
        FIELD_REPAIR_CODE = repairCode;
    }

    @NacosValue(value="${suning.field.repair.desc:疏通管道}", autoRefreshed = true)
    public void setFieldRepairDesc(String repairDesc) {
        FIELD_REPAIR_DESC = repairDesc;
    }

    @NacosValue(value="${suning.field.master.code:RY00138521}", autoRefreshed = true)
    public void setFieldMasterCode(String masterCode) {
        FIELD_MASTER_CODE = masterCode;
    }

    @NacosValue(value="${suning.field.master.name:一龙}", autoRefreshed = true)
    public void setFieldMasterName(String masterName) {
        FIELD_MASTER_NAME = masterName;
    }

    @NacosValue(value="${suning.field.master.phone:12112112100}", autoRefreshed = true)
    public void setFieldMasterPhone(String masterPhone) {
        FIELD_MASTER_PHONE = masterPhone;
    }

    @NacosValue(value="${suning.field.supplier.code:0028100002}", autoRefreshed = true)
    public void setFieldSupplierCode(String supplierCode) {
        FIELD_SUPPLIER_CODE = supplierCode;
    }

    @NacosValue(value="${suning.service.code_1:520500590}", autoRefreshed = true)
    public void setServiceProductCode1(String serviceProductCode) {
        SERVICE_PRODUCT_CODE_1 = serviceProductCode;
    }

    @NacosValue(value="${suning.service.code_2:520500589}", autoRefreshed = true)
    public void setServiceProductCode2(String serviceProductCode) {
        SERVICE_PRODUCT_CODE_2 = serviceProductCode;
    }

    @NacosValue(value="${suning.service.code_3:520500593}", autoRefreshed = true)
    public void setServiceProductCode3(String serviceProductCode) {
        SERVICE_PRODUCT_CODE_3 = serviceProductCode;
    }

    @NacosValue(value="${suning.service.code_4:520500592}", autoRefreshed = true)
    public void setServiceProductCode4(String serviceProductCode) {
        SERVICE_PRODUCT_CODE_4 = serviceProductCode;
    }
}
